/*
 * Cache.java
 *
 * Created on Jul 2, 2007, 11:06:46 PM
 *
 * Copyright (c) 2007 Daniel Gronau
 *
 * This file is part of KanjiTori.
 *
 * KanjiTori is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * KanjiTori is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

package kanjitori.util;

import com.jme.renderer.ColorRGBA;
import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.util.HashMap;
import java.util.Map;
import javax.swing.ImageIcon;

/**
 *
 * @author DGronau
 */
public class Cache {
    
    private static Map<String, Font> fontMap = new HashMap<String, Font>();
    private static Map<String, Color> colorMap = new HashMap<String, Color>();
    private static Map<String, ColorRGBA> colorRgbaMap = new HashMap<String, ColorRGBA>();
    private static Map<String, Image> imageMap = new HashMap<String, Image>();
    
    /** Static class */
    private Cache() {
    }
    
    public static Image getImage(String value) {
        if (value == null) {
            return null;
        }
 
        if (imageMap.containsKey(value)) {
            return imageMap.get(value);
        }
        Image image = new ImageIcon(value).getImage();
        imageMap.put(value, image);
        return image;
    }
 
    
    public static ColorRGBA getColorRGBA(String value) {
        if (value == null) {
            return null;
        }
        try {
            if (colorRgbaMap.containsKey(value)) {
                return colorRgbaMap.get(value);
            }
            String[] values = value.split(",");
            ColorRGBA colorRgba = new ColorRGBA(Float.parseFloat(values[0]),
                    Float.parseFloat(values[1]),
                    Float.parseFloat(values[2]),
                    Float.parseFloat(values[3]));
            colorRgbaMap.put(value, colorRgba);
            return colorRgba;
        } catch (Exception ex) {
            return null;
        }
    }
    
        public static ColorRGBA getColorRGBA(float r, float g, float b, float a) {
            String key = String.format("%f,%f,%f,%f", r, g, b, a);
            if (colorRgbaMap.containsKey(key)) {
                return colorRgbaMap.get(key);
            }
            ColorRGBA colorRgba = new ColorRGBA(r, g, b, a);
            colorRgbaMap.put(key, colorRgba);
            return colorRgba;
     }
    
    public static String colorRgbaString(ColorRGBA color) {
        return String.format("%f,%f,%f,%f", color.r, color.g, color.b, color.a);
    }
    
    public static Font getFont(String value) {
        if (value == null) {
            return null;
        }
        try {
            if (fontMap.containsKey(value)) {
                return fontMap.get(value);
            }
            String[] values = value.split(",");
            Font font = new Font(values[0], Integer.parseInt(values[1]), Integer.parseInt(values[2]));
            fontMap.put(value, font);
            return font;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
        public static Font getFont(String name, int style, int size) {
        String key = String.format("%s,%d,%d", name, style, size);
        try {
            if (fontMap.containsKey(key)) {
                return fontMap.get(key);
            }
            Font font = new Font(name, style, size);
            fontMap.put(key, font);
            return font;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }
    
    public static String fontString(Font font) {
        return String.format("%s,%d,%d", font.getName(), font.getStyle(), font.getSize());
    }    
    
    public static Color getColor(String value) {
        if (value == null) {
            return null;
        }
        try {
            if (colorMap.containsKey(value)) {
                return colorMap.get(value);
            }
            String[] values = value.split(",");
            Color color = new Color(Integer.parseInt(values[0]),
                    Integer.parseInt(values[1]),
                    Integer.parseInt(values[2]));
            colorMap.put(value, color);
            return color;
        } catch (Exception ex) {
            return null;
        }
    }
    
    public static Color getColor(int r, int g, int b) {
        String key = String.format("%d,%d,%d", r, g, b);
        if (colorMap.containsKey(key)) {
            return colorMap.get(key);
        }
        Color color = new Color(r, g, b);
        colorMap.put(key, color);
        return color;
    }
    
        public static String colorString(Color color) {
        return String.format("%d,%d,%d",
                color.getRed(),
                color.getGreen(),
                color.getBlue());
    }
}
